package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetMealMapper setMealMapper;

    /**
     * 新增套餐和对应的套餐和菜品的关系
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        //1.新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setMealMapper.insertSetMeal(setmeal);


        //2.新增套餐中的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();//获取套餐对应的菜品
        if(setmealDishes.size() == 0){
            return;
        }
        Long setmealId = setmeal.getId();//获取套餐id
        setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(setmealId);});//遍历集合设置套餐id

        setMealMapper.insertSetMealDishes(setmealDishes);
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageList(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setMealMapper.pageList(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //判断套餐是否时起售状态
        for (Long id : ids) {
            Setmeal setmeal = setMealMapper.findById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐菜品表中套餐和菜品的对应关系
        setMealMapper.deleteSetMealDishes(ids);

        //删除套餐
        setMealMapper.deleteSetMeal(ids);
    }

    /**
     * 根据id查询套餐数据
     * @param id
     * @return
     */
    @Override
    public SetmealVO findById(Long id) {
        //查询套餐信息
        Setmeal setmeal = setMealMapper.findById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);

        //查询套餐菜品信息
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setMealMapper.findDishes(setmealId);

        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 编辑套餐数据
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //编辑套餐
        setMealMapper.update(setmeal);

        //删除原本的菜品和套餐的关系
        Long setmealId = setmeal.getId();
        setMealMapper.deleteSetMealDish(setmealId);

        //重新添加菜品和套餐的对应关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes.size() == 0){
            return;
        }
        setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(setmealId);});
        setMealMapper.insertSetMealDishes(setmealDishes);
    }

    /**
     * 修改状态
     * @param status
     * @param id
     */
    @Override
    public void status(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setMealMapper.update(setmeal);
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> findByCategoryId(Integer categoryId) {
        return setMealMapper.findByCategoryId(categoryId);
    }

    @Autowired
    private DishMapper dishMapper;
    /**
     * 根据套餐id查询菜品
     */
    @Override
    public List<DishItemVO> dishesBySetMealId(Long setmealId){
        List<DishItemVO> list = new ArrayList<>();
        //根据套餐id查询对应的套餐菜品关系表
        List<SetmealDish> setmealDishes = setMealMapper.findDishes(setmealId);
        for (SetmealDish setmealDish : setmealDishes) {
            DishItemVO dishItemVO = new DishItemVO();
            dishItemVO.setCopies(setmealDish.getCopies());
            dishItemVO.setName(setmealDish.getName());
            Dish dish = dishMapper.findById(setmealDish.getDishId());
            dishItemVO.setDescription(dish.getDescription());
            dishItemVO.setImage(dish.getImage());
            list.add(dishItemVO);
        }
        return list;
    }
}
